<script setup>
import { GetDateByCodeAPI } from '@/api/common/index'
const { proxy } = getCurrentInstance()

const emit = defineEmits(['change', 'update:modelValue'])
const props = defineProps({
  disabled: { type: Boolean, default: false },
  modelValue: {
    type: String,
    default: '',
  },
  // 物料编码
  code: {
    type: String,
    default: '',
  },
})
const options = ref([])

const data = ref('')

// 获取数据 判断是否为单位或多单位 下拉数据
const getDateByCode = async () => {
  if (!props.code) {
    proxy.warningMsg('p-unit组件:未传入物料编码')
    return
  }
  const { data: res } = await GetDateByCodeAPI(props.code)
  options.value = res.sysAuxiliaryUnitDetailList
  if (res.sysAuxiliaryUnitDetailList?.length <= 1 && res.materialUnit) {
    emit('update:modelValue', res.materialUnit)
  }
}

watchEffect(() => {
  data.value = props.modelValue

  if (props.code) getDateByCode()
})

const change = (val) => {
  emit('update:modelValue', val)
}
</script>

<template>
  <!-- 单个 -->
  <el-select
    filterable
    :disabled="props.disabled"
    clearable
    v-model="data"
    placeholder="请选择单位"
    @change="change"
  >
    <el-option
      :label="item.unitName"
      :value="item.id"
      v-for="item in options"
      :key="item.value"
    />
  </el-select>
</template>
<style scoped lang="scss"></style>
